<?php

namespace Modules\Admin\Http\Controllers\Categories;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Modules\Common\General\Security\Api\Gateway;
use Modules\Common\Models\AdminLog;
use Modules\Common\Models\Categories;
use Modules\Common\Util\Api;
use Modules\Common\Util\Database;


class Retrieve
{

    /**
     * @Route(path=/api/admin/category)
     */
    public function retrieve()
    {
       
      $query = Categories::with(['adminUser'])
        ->withCount('ebook'); // 添加一个电子书数量的计数
    
        // 分类名称
        if (request('category_name')) {
            $query->where('category_name', 'like', '%' . request('category_name') . '%');
        }
    
        // 分类状态
        if (request('is_enabled')) {
            $query->where('is_enabled', '=', request('is_enabled'));
        }
        $query->orderByRaw('id desc , updated_at desc');
    
        $sidDefault = auth('api')->user()->sid;
        $eidDefault = auth('api')->user()->eid;
    
        // 获取分页数据
        $data = $query->where('sid', $sidDefault)->where('eid', $eidDefault)->paginate(request('per_page', 15));
 

        return Api::success($data);
    }

}